<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <style>
        .router-link-active{
            font-size: 20px;
            color: #f60;
        }
    </style>
</head>
<body>
<div id="box">
    <div>
        <router-link to="/home">主页</router-link>
        <router-link to="/news">新闻</router-link>
        <router-link to="/user">用户</router-link>
    </div>
    <div>
        <router-view></router-view>
    </div>

    <button @click="push">添加路由</button><br/>
    <p>
        添加路由相当于添加了一条历史记录 router.push({path:'/home'})
    </p>
    <button @click="replace">替换路由</button>
    <p>
        跟 router.push 很像，唯一的不同就是，它不会向 history 添加新记录，而是跟它的方法名一样 —— 替换掉当前的 history 记录。
    </p>
</div>

<template id="user">
    <div>
        <h3>我是用户信息</h3>
        <ul>
            <li><router-link to="/user/strive/age/10">strive</router-link></li>
            <li><router-link to="/user/blue/age/10">blue</router-link></li>
            <li><router-link to="/user/eric/age/10">eric</router-link></li>
        </ul>
        <div>
            <router-view></router-view>
        </div>
    </div>
</template>


<script>
    //组件
    var Home = {
        template: '<h3>我是主页</h3>'
    };
    var News = {
        template: '<h3>我是新闻</h3>'
    };
    var User = {
        template: '#user'
    };
    var UserDetail = {
        template: '<div>{{$route.params}}</div>'
    }


    //配置路由
    const routes=[
        {path: '/home',component: Home},
        {path: '/news',component: News},
        {
            path: '/user',
            component: User,
            children: [
                {path: ':usename/age/:age',component: UserDetail}
            ]
        },
        {path: '*', redirect: '/home'}
    ]

    //生成路由实例
    const router = new VueRouter({
        routes: routes
    })

    //挂载到vue上
    new Vue({
        router: router,
        el: '#box',
        methods:{
            push(){
                router.push({path:'/home'})
            },
            replace(){
                router.replace({path:'/user'});
            }
        }
    })
</script>
</body>
</html>